<form class="layui-form seller-form"  action="" >
    <div class="layui-form-item">
        <div class="layui-inline" style="display:none;">
            <label class="layui-form-label">账号：</label>
            <div class="layui-input-inline">
                <input type="text" name="username" lay-verify="title" style="width:100px;" placeholder="请输入账号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">手机号码：</label>
            <div class="layui-input-inline seller-inline-3">
                <input type="text" name="mobile" lay-verify="title" placeholder="请输入手机号码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">性别：</label>
            <div class="layui-input-inline seller-inline-2" >
                <select name="sex" lay-verify="">
                    <option value=""></option>
                    <option value="1">男</option>
                    <option value="2">女</option>
                    <option value="3">其他</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">生日：</label>
            <div class="layui-input-inline seller-inline-2">
                <input type="text" name="birthday" lay-verify="title" placeholder="生日日期" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">昵称：</label>
            <div class="layui-input-inline seller-inline-2">
                <input type="text" name="nickname" lay-verify="title" placeholder="用户昵称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">状态：</label>
            <div class="layui-input-inline seller-inline-3">
                <select name="status" lay-verify="">
                    <option value=""></option>
                    <option value="1">正常</option>
                    <option value="2">停用</option>
                </select>
            </div>
        </div>
        <!--<div class="layui-inline">
            <label class="layui-form-label">推荐人：</label>
            <div class="layui-input-inline seller-inline-3">
                <input type="text" name="pmobile" lay-verify="title" placeholder="推荐人手机号" autocomplete="off" class="layui-input">
            </div>
        </div>-->
        <div class="layui-inline">
	        <div class="layui-input-block">
	            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="*"><i class="iconfont icon-chaxun"></i>筛选</button>
	        </div>
        </div>
    </div>
</form>

<div class="table-body">
    <table id="userTable" lay-filter="test"></table>
</div>
<script type="text/html" id="form">
    <form class="layui-form" action="" style="padding: 30px;">
    <input type="hidden" name="id" value="{{ d.id }}" />
    <input type="hidden" name="type" value="{{ d.type }}" />
    <div class="layui-form-item">
        <label class="layui-form-label">余额</label>
        <div class="layui-input-block" style="line-height: 36px;">
          {{d.balance}}
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">
            {{# if(d.type == 'cz') { }} 充值 {{# } else {  }} 扣款 {{# } }}
        </label>
        <div class="layui-input-block">
            <input type="text" id="balance" name="balance"  placeholder="请输入余额" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">变更后余额</label>
        <div class="layui-input-block" id="money" style="line-height: 36px;">
          {{d.balance}}
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">
            备注
        </label>
        <div class="layui-input-block">
            <textarea name="meno" placeholder="请输入说明" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn" lay-submit lay-filter="save">立即提交</button>
           </div>
        </div>
    </form>
</script>
<script>
    layui.use(['form', 'layedit', 'laydate','table', 'laytpl'], function(){
        var laytpl = layui.laytpl
        layui.table.render({
            elem: '#userTable',
            height: 'full-260',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            url: "{:url('user/index')}?_ajax=1",
            id:'userTable',
            cols: [[
                {type:'numbers'},
//                {field:'username', width:100,title:'账号'},
                {field:'mobile', width:150, title:'手机号码'},
                {field:'sex', width:80, title:'性别'},
                {field:'birthday',width:150, title:'生日'},
                /*{field:'avatar',title:'头像',
                    templet: function(d){
                        if (d.avatar) {
                            return '<a href="javascript:void(0);" onclick=viewImage("'+d.avatar+'")><image style="max-width:30px;max-height:30px;" src="'+d.avatar+'"/></a>';
                        }else{
                            return '<a href="javascript:void(0);" onclick=viewImage("<?php echo config('jshop.default_image'); ?>")><image style="max-width:30px;max-height:30px;" src="<?php echo config('jshop.default_image');?>"/></a>';
                        }
                    }, width: 80
                },*/
                {field:'nickname',title:'昵称', width:150},
                {field:'balance',title:'余额',width:100},
                //{field:'point', sort:true, title:'积分', width: 120},
                {field:'status',title:'状态', width:150},
                //{field:'pid_name', title: '邀请人', width:150},
                {field:'ctime', title:'创建时间',width:170},
                {field:'option',title:'操作', width:180, templet: function (d) {
                    var html = '<span class="layui-btn layui-btn-xs balanceEdit"  data-id="'+d.id+'" data-balance="'+d.balance+'" data-type="cz" >充值</span>';
                   html += '<span class="layui-btn layui-btn-xs balanceEdit"  data-id="'+d.id+'" data-balance="'+d.balance+'" data-type="kk" >扣款</span>';
                    if(d.isbuy) {
                        html += '<span class="layui-btn layui-btn-danger layui-btn-xs lh"  data-id="'+d.id+'" data-is_buy="0" data-type="lh" >拉黑</span>';
                    } else {
                        html += '<span class="layui-btn layui-btn-warm layui-btn-xs lh"  data-id="'+d.id+'" data-is_buy="1" data-type="hf" >恢复</span>';
                    }
                    //html += '<span class="layui-btn layui-btn-xs inviteEdit" data-id="'+d.id+'">邀请人修改</span>';
                    //html += '<span class="layui-btn layui-btn-xs pointEdit" data-id="'+d.id+'">积分修改</span>';
                    //html += '<span class="layui-btn layui-btn-xs pointLog" data-id="'+d.id+'">积分详情</span>';
                    return html;
                }, align:'center',fixed:'right'}
            ]]
        });
        //搜索
        layui.form.on('submit(*)', function(data){
            layui.table.reload('userTable', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //积分编辑
        $(document).on('click', '.pointEdit', function () {
            var id = $(this).attr('data-id');
            $.ajax({
                url: "{:url('manage/user/editPoint')}",
                type: 'get',
                data: {
                    'user_id': id,
                    'flag': 'false'
                },
                success: function (e) {
                    layer.open({
                        type: 1,
                        title: '修改用户积分',
                        area: ['450px', '300px'], //宽高
                        content: e
                    });
                }
            });
        });
        //保存积分
        $(document).on('click', '.edit-point-save', function(){
            var user_id = $("#user_id").val();
            var point = $("#point").val();
            var memo = $("#memo").val();
            $.ajax({
                url: "{:url('manage/user/editPoint')}",
                type: 'post',
                data: {
                    'user_id': user_id,
                    'flag': 'true',
                    'point': point,
                    'memo': memo
                },
                success: function (e) {
                    if(e.status){
                        layer.msg(e.msg, {time: 1500}, function(){
                            layer.closeAll();
                            layui.table.reload('userTable');
                        });
                    }else{
                        layer.msg(e.msg);
                    }
                }
            });
        });
        //积分记录
        $(document).on('click', '.pointLog', function () {
            var id = $(this).attr('data-id');
            $.ajax({
                url: "{:url('manage/user/pointLog')}",
                type: 'get',
                data: {
                    'user_id': id,
                    'flag': 'false'
                },
                success: function (e) {
                    layer.open({
                        type: 1,
                        title: '用户积分记录',
                        area: ['800px', '535px'], //宽高
                        content: e
                    });
                }
            });
        });
        //邀请人修改
        $(document).on('click', '.inviteEdit', function () {
            var id = $(this).attr('data-id');
            layer.prompt({title: '请输入邀请人手机号', formType: 3}, function(mobile, index){
                $.ajax({
                    url: "{:url('manage/User/editInvite')}",
                    type: 'post',
                    dataType: 'json',
                    data: {
                        'id': id,
                        'mobile': mobile
                    },
                    success: function (e) {
                        if(e.status){
                            layer.close(index);
                            layer.msg(e.msg, {time: 1500}, function(){
                                layui.table.reload('userTable');
                            });
                        }else{
                            layer.msg(e.msg);
                        }
                    }
                });
            });
        });
        //拉黑&恢复
        $(document).on('click', '.lh', function () {
            var id = $(this).data('id');
            var isbuy = $(this).data('is_buy')
            $.ajax({
                type: 'POST',
                url: "{:url('isbuy')}",
                data: {id: id, isbuy: isbuy},
                dataType: 'JSON',
                 success: function(respon) {
                    layer.msg(respon.msg);
                    layui.table.reload('userTable');
                },
                error: function (respon) {
                    layer.msg(respon.responseJSON.msg, {icon: 5});
                }
            })
        })
        //修改余额
        $(document).on('click', '.balanceEdit', function () {
            var data = {}
            data.id = $(this).attr('data-id');
            data.balance = $(this).attr('data-balance')
            data.type = $(this).data('type')
            layer.prompt({
			  formType: 1,
			  title: '请输入密码',
			},function(val, index){
                $.ajax({
                    url: "{:url('balancePassword')}",
                    type: 'POST',
                    data: {password: val},
                    dataType: 'json',
                    success: function(respon) {
                        open(data)
                        layer.close(index);
                    },
                    error: function (respon) {
                        layer.msg(respon.responseJSON.msg, {icon: 5});
                    }
                })
                
            });
            
        })
        var open = function (data) {
            var form = $('#form').html();
            laytpl(form).render(data, function(html){
                lay_index = layer.open({
                    type: 1,
                    title: '修改余额',
                    shadeClose: true,
                    shade: false,
                    area: ['480px', '450px'], //宽高
                    content: html
                  });
                $('#balance').bind('input propertychange', function() {
                    var num = parseFloat($(this).val())
                    if (num.toString() == "NaN") { 
                        var total = parseFloat(data.balance);
                    } else {
                        if(data.type == 'kk') {
                            var total = parseFloat(data.balance)-num; 
                        }else {
                            var total = num+parseFloat(data.balance);
                        }
                    }
                    $('#money').html(total);
                });
            });
        }
        layui.form.on('submit(save)', function(data){
            layer.load(0, {shade: false});
            $.ajax({
                url: "{:url('balanceEdit')}",
                type: 'POST',
                data: data.field,
                dataType: 'json',
                success: function(respon) {
                    layer.closeAll();
                    layer.msg('操作成功', {time: 1500}, function(){
                        layui.table.reload('userTable');
                    });
                },
                error: function (respon) {
                    layer.msg(respon.responseJSON.msg, {icon: 5});
                }
            })
            return false;
        });
    });
</script>
